﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScriptCoreLib.JavaScript.DOM.HTML;
using ScriptCoreLib.JavaScript.Extensions;
using ScriptCoreLib.JavaScript.DOM;
using ScriptCoreLib.JavaScript;
using ScriptCoreLib.JavaScript.Runtime;
using System.Diagnostics;

namespace OrcasUltraWebApplication1
{
	public sealed class UltraApplication
	{
		public UltraApplication(IHTMLElement e)
		{
			new IHTMLDiv { innerHTML = "Hello world, Jaanus2!" }.AttachToDocument();

			{
				var btn = new IHTMLButton { innerText = "UltraWebService" }.AttachToDocument();

				btn.onclick +=
					delegate
					{

						new UltraWebService().GetTime("time: ",
							result =>
							{
								new IHTMLDiv { innerText = result }.AttachToDocument();

							}
						);

					};
			}

			{
				var btn = new IHTMLButton { innerText = "Load ExampleLibrary" }.AttachToDocument();

				btn.onclick +=
					delegate
					{
						btn.disabled = true;

						var s = new IHTMLScript { type = "text/javascript", src = "ExampleLibrary.js" };

						s.onload +=
							delegate
							{
								var f = new IFunction("e", "return ExampleLibrary_Method1(e);");

								var btn2 = new IHTMLButton { innerText = "Invoke ExampleLibrary_Method1" }.AttachToDocument();

								btn2.onclick +=
									delegate
									{
										f.apply(Native.Window, "hello world");
									};
							};

						s.AttachToDocument();
					};
			}

			{
				var c = new IHTMLDiv
				{
					innerHTML = @"
<table width='100%' style='height:100%;'>
  <tr><td valign='middle' align='center' height='100%'>
<table width='100%'  style='height:100%;'><tr><td height='100%'>
<h1>
Simple Scene Viewer
</h1>
<table id='container' width='90%' style='height:60%;' border='2'><tr><td height='100%'>
<div id='o3d' style='width: 100%; height: 100%;'></div>
</td></tr></table>
<p>
</p>

<input type='text' id='url' size='100'>
<input type='button' id='load' onclick='doload();' value='load'><BR>

Drag The Mouse To Rotate<br/>
Scrollwheel To Zoom<br/>
Resize The Window To Resize The View
<div style='color: red;' id='loading'></div>
</td></tr></table>
</td></tr></table>
"
				};

				c.AttachToDocument();


				Load(
					new string[]
							{
						"o3djs/base.js",
								"o3djs/util.js", 
								"o3djs/texture.js", 
								"o3djs/io.js", 
								
								"o3djs/event.js", 
								"o3djs/error.js", 
								"o3djs/math.js", "o3djs/quaternions.js", "o3djs/rendergraph.js", "o3djs/pack.js", "o3djs/material.js", "o3djs/effect.js", "o3djs/shape.js", "o3djs/element.js", "o3djs/arcball.js", "o3djs/scene.js", "o3djs/serialization.js"
							},
					delegate
					{
						var s2 = new IHTMLScript { type = "text/javascript", src = "SimpleViewer1.js" };

						s2.onload +=
							delegate
							{
								var init = new IFunction("return init();");
								var uninit = new IFunction("return uninit();");

								new Timer(
									t =>
									{
										//Debugger.Break();

										init.apply(Native.Window);

										Native.Window.onbeforeunload +=
											delegate
											{
												uninit.apply(Native.Window);
											};

										t.Stop();
									}
								, 1, 1);

							};

						s2.AttachToDocument();

					}
				);


			}
		}

		public static void Load(string[] e, Action done)
		{
			var i = 0;

			var LoadCurrent = default(Action);

			LoadCurrent = delegate
			{
				Native.Document.title = e[i];

				var s2 = new IHTMLScript { type = "text/javascript", src = e[i] };

				s2.onload +=
					delegate
					{
						i++;

						if (i == e.Length)
							done();
						else
							LoadCurrent();

					};

				s2.AttachToDocument();
			};

			LoadCurrent();
		}

	}

	public delegate void StringAction(string e);

	public sealed class UltraWebService
	{
		public void GetTime(string x, StringAction result)
		{
			result(x + DateTime.Now);
		}
	}

}
